package com.bittech.httpd.core;

import com.bittech.httpd.common.HttpRequest;
import com.bittech.httpd.common.HttpResponse;
import com.bittech.httpd.common.HttpStatus;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * 内部封装的响应类
 * Author: secondriver
 * Created: 2018/9/5
 */
public class DefaultHttpResponse implements HttpResponse {
    
    private final Socket socket;
    
    private final HttpRequest request;
    
    private HttpStatus httpStatus = HttpStatus.OK;
    
    private Map<String, String> header = new HashMap<>();
    
    private ByteArrayOutputStream content = new ByteArrayOutputStream();
    
    private Map<String, Object> context = new HashMap<>();
    
    public DefaultHttpResponse(Socket socket, HttpRequest request) {
        this.socket = socket;
        this.request = request;
    }
    
    @Override
    public Object getContextValue(String key) {
        return context.get(key);
    }
    
    @Override
    public void setHttpStatus(HttpStatus httpStatus) {
        this.httpStatus = httpStatus;
    }
    
    @Override
    public void setHeader(String key, String value) {
        this.header.put(key, value);
    }
    
    @Override
    public void setContentType(String value) {
        this.header.put("Content-Type", value);
    }
    
    @Override
    public void write(byte[] value) {
        try {
            content.write(value);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void write(String value) {
        try {
            content.write(value.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void flush() {
        //HTTP/1.1 200 OK
        //
        //header1 : value1
        //header2 : value2
        //
        //data
        try {
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(
                    encodeContent(
                            request.version()
                                    + " " +
                                    this.httpStatus.getRequestStatus()
                                    + " "
                                    + this.httpStatus.getDescription()
                                    + "\r\n"
                    
                    )
            );
            for (Map.Entry<String, String> entry : header.entrySet()) {
                outputStream.write(encodeContent(entry.getKey() + ": " + entry.getValue() + "\r\n"));
            }
            outputStream.write(encodeContent("\r\n"));
            ByteArrayInputStream inputStream = new ByteArrayInputStream(content.toByteArray());
            byte[] buff = new byte[1024];
            int len;
            while ((len = inputStream.read(buff)) != -1) {
                outputStream.write(buff, 0, len);
            }
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    
    public static byte[] encodeContent(String value) {
        return value.getBytes();
    }
    
    public void setContext(Map<String, Object> context) {
        this.context = context;
    }
}
